import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_bool_entity.dart';
import 'package:largefriends/entity/entities/lf_user_entity.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';

import '../../net/lf_api_url.dart';

toVisibility() {
  Get.toNamed(LFRoute.pathVisibility);
}

class LfVisibilityLogic extends GetxController {
  LfAuthService authService = Get.find();
  RxInt hideGender = 0.obs;
  RxInt hide = 0.obs;
  RxInt hideView = 0.obs;
  RxInt hideOnline = 0.obs;
  late LfUserEntity userEntity;

  @override
  void onInit() {
    userEntity = authService.currentAccount!;
    hideGender.value = userEntity.hiddenGender;
    hide.value = userEntity.hidden;
    hideView.value = userEntity.hiddenView;
    hideOnline.value = userEntity.hiddenOnline;
    super.onInit();
  }

  void changeHidden(int value, String type) {
    if (value != 0) {
      showActionTips(
          tips: strHideProfileTip.tr,
          actionButtonTip: strSure.tr,
          action: () {
            _requestHidden(value, type);
          });
    } else {
      _requestHidden(value, type);
    }
  }

  _requestHidden(int value, String type) {
    changePrivacy({'type': type, 'status': value}, () {
      hide.value = value;
      hideGender.value = 0;
      userEntity.hiddenGender = 0;
      userEntity.hidden = value;
      authService.setUserEntity(userEntity);
    }, () {
      /// donNothing
    });
  }

  void changeHiddenGender(int value) {
    if (value != 0) {
      showActionTips(
          tips: strHideProfileTip.tr,
          actionButtonTip: strSure.tr,
          action: () {
            _requestHiddenGender(value);
          });
    } else {
      _requestHiddenGender(value);
    }
  }

  _requestHiddenGender(int value) {
    changePrivacy({'type': 'hidden_gender', 'status': value}, () {
      hide.value = 0;
      hideGender.value = value;
      userEntity.hiddenGender = value;
      userEntity.hidden = 0;
      authService.setUserEntity(userEntity);
    }, () {
      /// donNothing
    });
  }

  void changePrivacy(dynamic param, Function() success, Function() failed) {
    showLoading();
    LfDio()
        .post<LfBoolEntity>(
      url: apiSetPrivacy,
      params: param,
    )
        .then((value) {
      hideLoading();
      success.call();
    }, onError: (error) {
      hideLoading();
      if (error is NetError) {
        showSnackBar(error.msg);
      }
    });
  }

  void changeAnonymously(int value) {
    if (value != 0) {
      showActionTips(
          tips: strBrowseAnonymously.tr,
          actionButtonTip: strSure.tr,
          action: () {
            _requestAnonymously(value);
          });
    } else {
      _requestAnonymously(value);
    }
  }

  _requestAnonymously(int value) {
    changePrivacy({'type': 'hidden_view', 'status': value}, () {
      hideView.value = value;
      userEntity.hiddenView = value;
      authService.setUserEntity(userEntity);
    }, () {
      /// donNothing
    });
  }

  void changeHiddenOnline(int value) {
    changePrivacy({'type': 'hidden_online', 'status': value}, () {
      hideOnline.value = value;
      userEntity.hiddenOnline = value;
      authService.setUserEntity(userEntity);
    }, () {
      /// donNothing
    });
  }
}
